package sng.modules.view.components.city.logic.unit
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.casalib.events.LoadEvent;
	import org.casalib.load.SwfLoad;
	
	import sng.modules.view.components.city.logic.event.TileEvent;
	import sng.modules.view.components.city.logic.interfaces.ISheet;
	
	public class SwfSheet extends Sprite implements ISheet
	{
		private var loader:SwfLoad;
		public var mc:MovieClip;
		private var frame:Object;
		
		private var path:String;		//royjin 20110711
		
		public function SwfSheet(_url:String, _frame:Object=null)
		{
			frame = _frame;
			load(_url);
		}
		
		protected function swfLoadedHandler(event:Event):void
		{
			mc = loader.contentAsMovieClip
			this.addChild(mc);
			if(frame!=null)
				mc.gotoAndStop(frame);
			dispatchEvent(new TileEvent(TileEvent.SKIN_LOAD_COMPLETE));
			loader.removeEventListener(LoadEvent.COMPLETE, swfLoadedHandler);
		}
		
		public function getMC():MovieClip
		{
//			if(this.numChildren==0) return null;
			return mc;
		}
		
		public function load(url:String):void
		{
			this.path = url;
			//todo 统一管理载入的资源
			loader = new SwfLoad(url);
			loader.start();
			loader.addEventListener(LoadEvent.COMPLETE, swfLoadedHandler);
		}
		
		
		public function addAnimation(name:String, frames:Array, frameRate:Number=30, looped:Boolean=false):void
		{
		}
		
		public function render():void
		{
		}
		
		//重新加载新的资源 royjin 20110711
		public function Reload(url:String):void
		{
			//如果当前皮肤路径和需要新加载的一样
			if(this.path==url)
			{
				return;
			}
			//清理当前mc
			if(mc!=null)
			{
				mc.gotoAndStop(0);
				if(mc.parent==this)
				{
					this.removeChild(mc);
				}
			}
			//清理当前loader
			if(loader!=null)
			{
				loader.removeEventListener(LoadEvent.COMPLETE, swfLoadedHandler);
				loader.destroy();
				loader=null;
				this.load(url);
			}
		}
	}
}